package com.harisucici.websocketlib.im;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

import com.harisucici.websocketlib.broadcast.ChatMessageReceiver;
import com.harisucici.websocketlib.broadcast.ChatStatusReceiver;

import static android.content.Context.BIND_AUTO_CREATE;

/**
 * 一般工具类
 * Created by harisucici on 2017/6/13.
 */
@Deprecated
public class HWebSocketManager {
    private static HWebSocketManager manager;
    private Context context;
    private String url;
    private HWebSocketClientService.HWebSocketClientBinder binder;
    public HWebSocketClientService jWebSClientService;
    public HWebSocketClient client;
    private ChatStatusReceiver chatStatusReceiver;
    private ChatMessageReceiver chatMessageReceiver;
    private IntentFilter filter = new IntentFilter(HWebSocketClientService.BROADCAST_ACTION);
    private long heart;

    public static synchronized HWebSocketManager getInstance() {
        if (manager == null) {
            manager = new HWebSocketManager();

        }
        return manager;
    }


    public void onClose() {
        jWebSClientService.closeConnect();
        onRelease();
    }

    private void onRelease() {
        try {
            if (manager != null) {
                manager = null;
            }
        } catch (Exception e) {
        }
    }


    //    public void initHWebSocketService(Context context, String url, ChatStatusReceiver chatStatusReceiver,ChatMessageReceiver chatMessageReceiver) {
//        this.context = context;
//        this.url = url;
//        this.chatStatusReceiver = chatStatusReceiver;
//        this.chatMessageReceiver = chatMessageReceiver  ;
//        //启动服务
//        startJWebSClientService();
//        //绑定服务
//        bindService();
//        //注册广播
//        doRegisterReceiver();
//
//    }
    public void initHWebSocketService(Context context, String url,long heart, ChatStatusReceiver chatStatusReceiver,ChatMessageReceiver chatMessageReceiver) {
        this.context = context;
        this.url = url;
        this.heart = heart;
        this.chatStatusReceiver = chatStatusReceiver;
        this.chatMessageReceiver = chatMessageReceiver  ;
        //启动服务
        startJWebSClientService();
        //绑定服务
        bindService();
        //注册广播
        doRegisterReceiver();

    }

    //启动服务,传递 url
    public void startJWebSClientService() {
        Intent intent = new Intent(context, HWebSocketClientService.class);
        intent.putExtra(HWebSocketClientService.URL, url);
        intent.putExtra(HWebSocketClientService.HEART, heart);
        context.startService(intent);
    }

    //绑定服务
    public void bindService() {
        Intent bindIntent = new Intent(context, HWebSocketClientService.class);
        context.bindService(bindIntent, serviceConnection, BIND_AUTO_CREATE);
    }


    // 注册广播
    public void doRegisterReceiver() {
        //消息
        if(chatMessageReceiver==null){
            Log.i("MainViewModel", "需要先绑定收信广播!!");
            return;
        }
        IntentFilter filter = new IntentFilter(HWebSocketClientService.BROADCAST_ACTION);
        context.registerReceiver(chatMessageReceiver, filter);
        //网络状态
        IntentFilter filterStatus = new IntentFilter(HWebSocketClientService.BROADCAST_ACTION_STATUS);
        context.registerReceiver(chatStatusReceiver, filterStatus);
    }


    public ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.i("MainViewModel", "服务与活动绑定");
            binder = (HWebSocketClientService.HWebSocketClientBinder) iBinder;
            jWebSClientService = binder.getService();
            client = jWebSClientService.getClient();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.i("MainViewModel", "服务与活动断开");
        }
    };


    public HWebSocketClientService getService() {
        return jWebSClientService;
    }


}

